
--require "nettest"
--require "synctest"
--require "bugtest"

local require = require
local loadfile = loadfile
local setfenv = setfenv

local env = {}

env._G = env

for _,g in ipairs({
	"assert", "ipairs", "pairs", "next", "pcall", "tonumber", "tostring",
	"type", "unpack", "print", "error", -- TODO: Is error bad?
	"coroutine", "string", "math", "fdlibm", "table", -- Don't care about table modification
	"bit", "serialize",
	"io", -- TEMP
	--"flip", "psync", "box2d", "portaudio",

	"test", "jit",

	-- TODO: Make sure metatables cannot leak anything bad, using __metatable if necessary.
	-- We don't care about the sandbox being able to modify metatables.
	"getmetatable", "setmetatable"
	}) do
	env[g] = _G[g]
end

env["graphics"] = _G["flip"]
env["network"] = _G["psync"]
env["physics"] = _G["box2d"]
env["sound"] = _G["portaudio"]
env["filesystem"] = _G["filesystem"]
env["random"] = _G["random"]
env["system"] = _G["system"]

env.os = {clock = os.clock}

local function sandbox(f)
	setfenv(f, env)
	f()
end

sandbox(function()
	env.require = function(path)
		-- TODO: This is temporary, we need it to be more restricted
		local f, err = loadfile(path .. ".lua")
		if not f then
			error("loadfile error: " .. err)
		end
		setfenv(f, env)
		return f()
	end
	--env.require "gfxtest"
	--env.require "gametest"
	--env.require "nettest"
	--env.require "synctest"
	--env.require "drawtest"
	--env.require "bitstreamtest"
	--env.require "functest"
	env.require "streamstest"
	--env.require "randomtest"
	--gfx_test()
	--net_test()
	--draw_test()
end)